package com.qyhl.module_practice.study.detail;

import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.qyhl.webtv.commonlib.entity.news.UnionBean;

import java.util.List;

public class PracticeNewStudyModel implements PracticeNewStudyContract.PracticeNewStudyModel {
    private PracticeNewStudyPresenter mPresenter;

    PracticeNewStudyModel(PracticeNewStudyPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getSectionId(String id) {
        EasyHttp.get(PracticeUrl.GET_TOWN_LIST)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("type", "1")
                .params("setions", id)
                .execute(new SimpleCallBack<List<UnionBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setIdResult("");
                    }

                    @Override
                    public void onSuccess(List<UnionBean> unionBeans) {
                        if (unionBeans != null && unionBeans.size() > 0) {
                            mPresenter.setIdResult(unionBeans.get(0).getId());
                        } else {
                            mPresenter.setIdResult("");
                        }
                    }
                });
    }

    @Override
    public void getData(String sectionId, String newsId) {
        EasyHttp.get(PracticeUrl.GET_HOME_NEWS)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("InnerCode", sectionId)
                .params("ID", newsId)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .execute(new SimpleCallBack<List<NewsBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无任何内容！", !newsId.equals("0"));
                        } else {
                            mPresenter.setError("请求出错，请重新获取！", !newsId.equals("0"));
                        }
                    }

                    @Override
                    public void onSuccess(List<NewsBean> practiceAcitivityBeans) {
                        if (practiceAcitivityBeans != null && practiceAcitivityBeans.size() > 0) {
                            mPresenter.setData(practiceAcitivityBeans, !newsId.equals("0"));
                        } else {
                            mPresenter.setError("暂无任何内容！", !newsId.equals("0"));
                        }
                    }
                });
    }
}
